#include <CpLog.h>
#include "CpPolynom2D.h"
#include <TMath.h>

ClassImp(CpPolynom2D);

//______________________________________________________________________________
CpPolynom2D::CpPolynom2D(): TObject() {
//
// Constructor
//

  CpDebug(CpLog::kDebug + 5, "<-");
  CpDebug(CpLog::kDebug + 5, "->");

}

//______________________________________________________________________________
CpPolynom2D::~CpPolynom2D() {
//
// Destructor.
//
  CpDebug(CpLog::kDebug + 5, "<-");
  CpDebug(CpLog::kDebug + 5, "->");
}


//______________________________________________________________________________
Int_t CpPolynom2D::GetNumberOfParameters(Int_t grade) {
//
// Gets Number of parameters
//
//     CpDebug(CpLog::kDebug+5,"<-");
  switch (grade) {
    case 0:  return 1;
    case 1:  return 3;
    case 2:  return 6;
    case 3:  return 10;
    case 4:  return 15;
    case 5:  return 21;
    case 6:  return 28;
    case 7:  return 36;
    case 8:  return 45;
    case 9:  return 55;
    case 10: return 66;
//     case 11: return 12;
    default : return -1;
    
  }
  
//     CpDebug(CpLog::kDebug+5,"->");
}


Double_t CpPolynom2D::DoPolynom0(Double_t* x, Double_t* par) {
  Double_t r = par[0] + x[0] * 0;
  return r;
}


Double_t CpPolynom2D::DoPolynom1(Double_t* x, Double_t* par) {

  Double_t r = DoPolynom0(x, par);;
  r += par[1] * TMath::Power(x[0], 1);
  r += par[2] * TMath::Power(x[1], 1);
  return r;
}


Double_t CpPolynom2D::DoPolynom2(Double_t* x, Double_t* par) {
  Double_t r = DoPolynom1(x, par);
  // x^2 y^2
  r += par[3] * TMath::Power(x[0], 2);
  r += par[4] * TMath::Power(x[1], 2);
  r += par[5] * TMath::Power(x[0], 1) * TMath::Power(x[1], 1);
  return r;
}


Double_t CpPolynom2D::DoPolynom3(Double_t* x, Double_t* par) {
  Double_t r = DoPolynom2(x, par);
  // x^3 y^3 x^2*y x*y^2 x*y^3
  r += par[6] * TMath::Power(x[0], 3);
  r += par[7] * TMath::Power(x[1], 3);
  r += par[8] * TMath::Power(x[0], 2) * TMath::Power(x[1], 1);
  r += par[9] * TMath::Power(x[0], 1) * TMath::Power(x[1], 2);

  return r;
}

Double_t CpPolynom2D::DoPolynom4(Double_t* x, Double_t* par) {
  Double_t r = DoPolynom3(x, par);;
  // x^4 y^4 x^3*y x^2*y^2 x*y^3
  r += par[10] * TMath::Power(x[0], 4);
  r += par[11] * TMath::Power(x[1], 4);
  r += par[12] * TMath::Power(x[0], 3) * TMath::Power(x[1], 1);
  r += par[13] * TMath::Power(x[0], 2) * TMath::Power(x[1], 2);
  r += par[14] * TMath::Power(x[0], 1) * TMath::Power(x[1], 3);
  return r;
}


Double_t CpPolynom2D::DoPolynom5(Double_t* x, Double_t* par) {
  Double_t r = DoPolynom4(x, par);;
// x^5 y^5 x^4*y x^3*y^2 x^2*y^3 x*y^4
  r += par[15] * TMath::Power(x[0], 5);
  r += par[16] * TMath::Power(x[1], 5);
  r += par[17] * TMath::Power(x[0], 4) * TMath::Power(x[1], 1);
  r += par[18] * TMath::Power(x[0], 3) * TMath::Power(x[1], 2);
  r += par[19] * TMath::Power(x[0], 2) * TMath::Power(x[1], 3);
  r += par[20] * TMath::Power(x[0], 1) * TMath::Power(x[1], 4);
  return r;
}

Double_t CpPolynom2D::DoPolynom6(Double_t* x, Double_t* par) {
  Double_t r = DoPolynom5(x, par);;
  // x^6 y^6 x^5*y x^4*y^2 x^3*y^3 x^2*y^4 x*y^5
  r += par[21] * TMath::Power(x[0], 6);
  r += par[22] * TMath::Power(x[1], 6);
  r += par[23] * TMath::Power(x[0], 5) * TMath::Power(x[1], 1);
  r += par[24] * TMath::Power(x[0], 4) * TMath::Power(x[1], 2);
  r += par[25] * TMath::Power(x[0], 3) * TMath::Power(x[1], 3);
  r += par[26] * TMath::Power(x[0], 2) * TMath::Power(x[1], 4);
  r += par[27] * TMath::Power(x[0], 1) * TMath::Power(x[1], 5);
  return r;
}

Double_t CpPolynom2D::DoPolynom7(Double_t* x, Double_t* par) {
  Double_t r = DoPolynom6(x, par);;
  // x^7 y^7 x^6*y x^5*y^2 x^4*y^3 x^3*y^4 x^2*y^5 x*y^6
  r += par[28] * TMath::Power(x[0], 7);
  r += par[29] * TMath::Power(x[1], 7);
  r += par[30] * TMath::Power(x[0], 6) * TMath::Power(x[1], 1);
  r += par[31] * TMath::Power(x[0], 5) * TMath::Power(x[1], 2);
  r += par[32] * TMath::Power(x[0], 4) * TMath::Power(x[1], 3);
  r += par[33] * TMath::Power(x[0], 3) * TMath::Power(x[1], 4);
  r += par[34] * TMath::Power(x[0], 2) * TMath::Power(x[1], 5);
  r += par[35] * TMath::Power(x[0], 1) * TMath::Power(x[1], 6);
  return r;
}


Double_t CpPolynom2D::DoPolynom8(Double_t* x, Double_t* par) {
  Double_t r = DoPolynom7(x, par);;
  // x^8 y^8 x^7*y x^6*y^2 x^5*y^3 x^4*y^4 x^3*y^5 x^2*y^6 x*y^7
  r += par[36] * TMath::Power(x[0], 8);
  r += par[37] * TMath::Power(x[1], 8);
  r += par[38] * TMath::Power(x[0], 7) * TMath::Power(x[1], 1);
  r += par[39] * TMath::Power(x[0], 6) * TMath::Power(x[1], 2);
  r += par[40] * TMath::Power(x[0], 5) * TMath::Power(x[1], 3);
  r += par[41] * TMath::Power(x[0], 4) * TMath::Power(x[1], 4);
  r += par[42] * TMath::Power(x[0], 3) * TMath::Power(x[1], 5);
  r += par[43] * TMath::Power(x[0], 2) * TMath::Power(x[1], 6);
  r += par[44] * TMath::Power(x[0], 1) * TMath::Power(x[1], 7);
  return r;
}


Double_t CpPolynom2D::DoPolynom9(Double_t* x, Double_t* par) {
  Double_t r = DoPolynom8(x, par);;
  // x^9 y^9 x^8*y x^7*y^2 x^6*y^3 x^5*y^4 x^4*y^5 x^3*y^6 x^2*y^7 x*y^8
  r += par[45] * TMath::Power(x[0], 9);
  r += par[46] * TMath::Power(x[1], 9);
  r += par[47] * TMath::Power(x[0], 8) * TMath::Power(x[1], 1);
  r += par[48] * TMath::Power(x[0], 7) * TMath::Power(x[1], 2);
  r += par[49] * TMath::Power(x[0], 6) * TMath::Power(x[1], 3);
  r += par[50] * TMath::Power(x[0], 5) * TMath::Power(x[1], 4);
  r += par[51] * TMath::Power(x[0], 4) * TMath::Power(x[1], 5);
  r += par[52] * TMath::Power(x[0], 3) * TMath::Power(x[1], 6);
  r += par[53] * TMath::Power(x[0], 2) * TMath::Power(x[1], 7);
  r += par[54] * TMath::Power(x[0], 1) * TMath::Power(x[1], 8);
  return r;
}
Double_t CpPolynom2D::DoPolynom10(Double_t* x, Double_t* par) {
  Double_t r = DoPolynom9(x, par);;
  // x^10 y^10 x^9*y x^8*y^2 x^7*y^3 x^6*y^4 x^5*y^5 x^4*y^6 x^3*y^7 x^2*y^8 x*y^9
  r += par[55] * TMath::Power(x[0], 10);
  r += par[56] * TMath::Power(x[1], 10);
  r += par[57] * TMath::Power(x[0], 9) * TMath::Power(x[1], 1);
  r += par[58] * TMath::Power(x[0], 8) * TMath::Power(x[1], 2);
  r += par[59] * TMath::Power(x[0], 7) * TMath::Power(x[1], 3);
  r += par[60] * TMath::Power(x[0], 6) * TMath::Power(x[1], 4);
  r += par[61] * TMath::Power(x[0], 5) * TMath::Power(x[1], 5);
  r += par[62] * TMath::Power(x[0], 4) * TMath::Power(x[1], 6);
  r += par[63] * TMath::Power(x[0], 3) * TMath::Power(x[1], 7);
  r += par[64] * TMath::Power(x[0], 2) * TMath::Power(x[1], 8);
  r += par[65] * TMath::Power(x[0], 1) * TMath::Power(x[1], 9);
  return r;
}


